// UEXData.cpp: implementation of the UEXData class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UEXData.h"
#include <geassign.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4800)

UEXData::UEXData(const CString& strName)
{
	ASSERT(!strName.IsEmpty());
	m_strName = strName;
	m_buf.Restype(1001);
	m_buf.resval.rstring = (TCHAR*)malloc(m_strName.GetLength() + 1);
	memcpy(m_buf.resval.rstring, m_strName.GetBuffer(0), (m_strName.GetLength() + 1)*sizeof(TCHAR));


	m_nState = eNOT;
	m_pPos = NULL;
	m_pPrevPos = NULL;
	m_pLastBuf = &m_buf;
}

UEXData::~UEXData()
{
	
}

void UEXData::Add(struct resbuf* pRb)
{
	m_buf.Add(*pRb);
	while(m_pLastBuf->Next() != NULL)
	{
		m_pLastBuf = m_pLastBuf->Next();
	}
}


void UEXData::WriteAcDbHandle(const AcDbHandle& handle)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdAsciiString);
	m_pPos->resval.rstring = (char*)malloc(17*sizeof(char));
	handle.getIntoAsciiBuffer(m_pPos->resval.rstring);

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WriteBool(bool bData)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdInteger32);
	m_pPos->resval.rint = bData;

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WriteDouble(DOUBLE dbData)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdReal);
	m_pPos->resval.rreal = dbData;

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WriteInt(INT nData)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdInteger32);
	m_pPos->resval.rint = nData;

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WritePoint3d(const AcGePoint3d& pt)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdWorldXCoord);
	memcpy(m_pPos->resval.rpoint, asDblArray(pt), sizeof(m_pPos->resval.rpoint));

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WriteString(const CString& strData)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdAsciiString);
	m_pPos->resval.rstring = (TCHAR*)malloc(strData.GetLength() + 1);
	CString strTemp;
	strTemp = strData;
	memcpy(m_pPos->resval.rstring, strTemp.GetBuffer(0), (strData.GetLength() + 1)*sizeof(TCHAR));
	strTemp.ReleaseBuffer();

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

void UEXData::WriteVector3d(const AcGeVector3d& vector)
{
	if(m_nState != eWRITE)
	{
		ASSERT(FALSE);
		return;
	}
	
	if(m_pPos == NULL)
	{
		m_pPos = new UeResbuf;
		ASSERT(m_pPos);
		m_pPrevPos->Next(m_pPos);
		m_pLastBuf = m_pPos;
	}
	m_pPos->Restype(AcDb::kDxfXdWorldXCoord);
	memcpy(m_pPos->resval.rpoint, asDblArray(vector), sizeof(m_pPos->resval.rpoint));

	m_pPrevPos = m_pPos;
	m_pPos = m_pPos->Next();
}

BOOL UEXData::ReadAcDbHandle(AcDbHandle& handle)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdAsciiString)
	{
		handle = AcDbHandle(m_pPos->resval.rstring);
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadBool(bool& bData)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdInteger32)
	{
		bData = static_cast<bool>(m_pPos->resval.rint);
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadDouble(DOUBLE& dbData)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdReal)
	{
		dbData = m_pPos->resval.rreal;
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadInt(INT& nData)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdInteger32)
	{
		nData = m_pPos->resval.rint;
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadPoint3d(AcGePoint3d& pt)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdWorldXCoord)
	{
		pt = asPnt3d(m_pPos->resval.rpoint);
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadString(CString& strData)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdAsciiString)
	{
		strData = m_pPos->resval.rstring;
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

BOOL UEXData::ReadVector3d(AcGeVector3d& vector)
{
	BOOL bResult = FALSE;
	if(m_nState != eREAD || m_pPos == NULL)
	{
		ASSERT(FALSE);
		return bResult;
	}
	
	if(m_pPos->Restype() == AcDb::kDxfXdWorldXCoord)
	{
		vector = asVec3d(m_pPos->resval.rpoint);
		bResult = TRUE;
	}
	m_pPos = m_pPos->Next();
	return bResult;
}

void UEXData::AssertReadEnable()
{
	m_nState = eREAD;
	m_pPos = m_buf.Next();
}

void UEXData::AssertWriteEnable()
{
	m_nState = eWRITE;
	m_pPrevPos = &m_buf;
	m_buf.SetNext(NULL);
	m_pPos = NULL;
}